package cn.openea.perpartner.timeline.web.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.timeline.web.entity.InsTicket;
import cn.openea.perpartner.timeline.web.entity.dto.SearchDto;
import cn.openea.perpartner.timeline.web.mapper.InsTicketMapper;
import cn.openea.perpartner.timeline.web.service.InsTicketService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (InsTicket)表服务实现类
 *
 * @author Weird077
 * @since 2022-12-11 22:16:28
 */
@Service("insTicketService")
public class InsTicketServiceImpl extends ServiceImpl<InsTicketMapper, InsTicket> implements InsTicketService {

    @Override
    public List<InsTicket> selectAll(SearchDto dto) {
        QueryWrapper<InsTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("a.create_user", dto.getUserId());
        if (StrUtil.isNotBlank(dto.getSearchStr())) {
            wrapper.and(wrap -> wrap.like("a.ticket_name", dto.getSearchStr())
                    .or().like("a.ticket_number", dto.getSearchStr())
                    .or().like("a.ticket_details", dto.getSearchStr())
                    .or().like("a.create_user_name", dto.getSearchStr()));
        }
        wrapper.orderByDesc("a.create_time");
        return baseMapper.selectTicketList(wrapper);
    }

    @Override
    public List<InsTicket> selectCreatedList(SearchDto dto) {
        LambdaQueryWrapper<InsTicket> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InsTicket::getUserId, dto.getUserId());
        if (StrUtil.isNotBlank(dto.getSearchStr())) {
            wrapper.and(wrap -> wrap.like(InsTicket::getTicketName, dto.getSearchStr())
                    .or().like(InsTicket::getTicketDetails, dto.getSearchStr())
                    .or().like(InsTicket::getTicketNumber, dto.getSearchStr())
                    .or().like(InsTicket::getNickName, dto.getSearchStr()));
        }
        wrapper.orderByDesc(InsTicket::getCreateTime);
        return list(wrapper);
    }
}

